package com.youxue.teachin.youmen;

import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.Map;

import android.app.Activity;
import android.app.Application;
import android.app.Application.ActivityLifecycleCallbacks;
import android.content.Context;
import android.os.Bundle;
import android.text.TextUtils;
import com.umeng.analytics.MobclickAgent;
import com.umeng.analytics.MobclickAgent.EScenarioType;

/**
 * 友盟第三方sdk
 *
 * @author: zhiwei
 * @date: 2017-10-15 21:08
 * @version: 9.1.0
 */
public class UMenUtils {
    public static class UmenConfig {
        /**
         * ID：用户账号ID，长度小于64字节
         */
        public String ProfileSignInId = "unknow";
        /**
         * 账号来源。如果用户通过第三方账号登陆，可以调用此接口进行统计。支持自定义，不能以下划线"_"开头，
         * 使用大写字母和数字标识，长度小于32 字节; 如果是上市公司，建议使用股票代码。
         */
        public String ProfileSignInProvider = "unknow";

        public boolean DEBUG = false;

    }

    public static Application mApplication;
    public static UmenConfig sYoumenConfig = new UmenConfig();

    public static void reportError(Context context, Throwable e) {
        MobclickAgent.reportError(context, e);
    }

    public static void updateConfig(UmenConfig youmenConfig) {
        sYoumenConfig = youmenConfig;

        onProfileSignIn();
    }

    public static void onEvent(String eventId, Map<String, String> map) {
        MobclickAgent.onEvent(mApplication, eventId, map);
    }

    /**
     *
     * @param close flase 关闭页面统计
     */
    public static void openActivityDurationTrack(boolean close) {
        MobclickAgent.openActivityDurationTrack(close);
    }

    public static void onPageStart(String page) {
        MobclickAgent.onPageStart(page);
    }

    public static void onPageEnd(String page) {
        MobclickAgent.onPageEnd(page);
    }

    public static void onProfileSignIn() {
        MobclickAgent.onProfileSignIn(sYoumenConfig.ProfileSignInProvider, sYoumenConfig.ProfileSignInId);
    }

    public static void onProfileSignOff() {
        MobclickAgent.onProfileSignOff();
    }

    public static void onKillProcess() {
        MobclickAgent.onKillProcess(mApplication);
    }

    public static void init(final Application context, UmenConfig youmenConfig) {
        mApplication = context;
        if (youmenConfig != null) {
            sYoumenConfig = youmenConfig;
        }
        //普通统计场景类型
        MobclickAgent.setScenarioType(context, EScenarioType.E_UM_NORMAL);
        /**
         * 推到后台后30秒后再次打开认为是一次回话
         */
        MobclickAgent.setSessionContinueMillis(30 * 1000);
        /**
         * 加密传输日志
         */
        MobclickAgent.enableEncrypt(true);

        MobclickAgent.setDebugMode(sYoumenConfig.DEBUG);

        context.registerActivityLifecycleCallbacks(new ActivityLifecycleCallbacks() {
            @Override
            public void onActivityCreated(Activity activity, Bundle savedInstanceState) {

            }

            @Override
            public void onActivityStarted(Activity activity) {

            }

            @Override
            public void onActivityResumed(Activity activity) {
                if (activity.getClass().getSimpleName().equals("MainActivity")) {
                    return;
                }
                MobclickAgent.onResume(activity);
            }

            @Override
            public void onActivityPaused(Activity activity) {
                if (activity.getClass().getSimpleName().equals("MainActivity")) {
                    return;
                }
                MobclickAgent.onPause(activity);
            }

            @Override
            public void onActivityStopped(Activity activity) {

            }

            @Override
            public void onActivitySaveInstanceState(Activity activity, Bundle outState) {

            }

            @Override
            public void onActivityDestroyed(Activity activity) {

            }
        });

    }

    public static String getDeviceInfo(Context context) {
        try {
            org.json.JSONObject json = new org.json.JSONObject();
            android.telephony.TelephonyManager tm = (android.telephony.TelephonyManager)context
                .getSystemService(Context.TELEPHONY_SERVICE);
            String device_id = null;
            device_id = tm.getDeviceId();
            String mac = null;
            FileReader fstream = null;
            try {
                fstream = new FileReader("/sys/class/net/wlan0/address");
            } catch (FileNotFoundException e) {
                fstream = new FileReader("/sys/class/net/eth0/address");
            }
            BufferedReader in = null;
            if (fstream != null) {
                try {
                    in = new BufferedReader(fstream, 1024);
                    mac = in.readLine();
                } catch (IOException e) {
                } finally {
                    if (fstream != null) {
                        try {
                            fstream.close();
                        } catch (IOException e) {
                            e.printStackTrace();
                        }
                    }
                    if (in != null) {
                        try {
                            in.close();
                        } catch (IOException e) {
                            e.printStackTrace();
                        }
                    }
                }
            }
            json.put("mac", mac);
            if (TextUtils.isEmpty(device_id)) {
                device_id = mac;
            }
            if (TextUtils.isEmpty(device_id)) {
                device_id = android.provider.Settings.Secure.getString(context.getContentResolver(),
                    android.provider.Settings.Secure.ANDROID_ID);
            }
            json.put("device_id", device_id);
            return json.toString();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }
}
